#!/usr/bin/env php
<?php
/**
 * @name conf.php
 * @desc odp-cg的配置文件
 * @author Wei He(he_wei@baidu.com)
 * @modifier Laruence(laruence@baidu.com)
 */

$options = getOpt("a:d:nh", array("namespace", "directory:", "app", "help"));

if (empty($options) || isset($options["h"]) || isset($options["help"])) {
    die(<<<HELP
Usage: yaf_cg [-a ApplicationName] [-d ApplicationPath] [-n]

POSIX options:  GNU long options:    Meaning:
      -a          --app              Applicaton Name, default yaf_skeleton
      -d          --directory        Output directory, default ./ApplicationName
      -n          --namespace        Using Namespace Style
      -h          --help             Print Usage

HELP
);
}

if (!isset($options["a"]) && !isset($options["app"])) {
	$appname = "yaf_skeleton";
} else {
	$appname = isset($options["a"])? strval($options["a"]) : strval($options["app"]);
}

if (!isset($options["d"]) && !isset($options["directory"])) {
	$directory = __DIR__ . "/yaf_skeleton";
} else {
	$directory = isset($options["d"])? strval($options["d"]) : strval($options["directory"]);
}

if (!isset($options["n"]) && !isset($options["namespace"])) {
	$use_namespace = 0;
} else {
	$use_namespace = 1;
}

echo "Outputing", $use_namespace? " Namspaced" : "", " Yaf Skeleton to {$directory}\n";

$author = trim(`whoami`);
$hostname = trim(`hostname`);

$conf= array (
    'author'     => $author,        //开发人员
    'app_name'   => $appname,       //app名字,请确保这也是一个合法的文件夹名
	'directory'  => $directory,     //输出的目标地址
	'namespace'  => $use_namespace, //是否启用命名空间申明方式，比如Yaf_Appliation会变成Yaf\Application
    'dev_pc'     => $author . '@' . $hostname ,//开发机,用于生产makefile自动部署
);

generateSkeleton($conf);

function generateSkeleton($conf) {
	$skeletons = getTemplates(__DIR__ . "/templates/");
	processTemplates($skeletons, $conf);
	processOutput($conf["directory"], $skeletons);
}

function processTemplates(&$skeletons, $conf) {
	foreach ($skeletons as $k => &$v) {
		if (is_array($v)) {
			processTemplates($v, $conf);
		} else if (substr($k, -7) != "ini.tpl") {
			$v = str_replace(
				array(
					'{&$AUTHOR&}',
					'{&$APP_NAME&}',
					'{&$DEV_PC&}'
				),
				array(
					$conf["author"],
					$conf["app_name"],
					$conf["dev_pc"]
				),
				$v);

			if ($conf["namespace"]) {
				$v = str_replace(
					array(
						"Yaf_Application",
						"Yaf_Dispatcher",
						"Yaf_Controller_Abstract",
						"Yaf_Bootstrap_Abstract",
						"Yaf_Plugin_Abstract",
						"Yaf_Request_Abstract",
						"Yaf_Response_Abstract",
						"Yaf_Registry",
					),
					array(
						"\\Yaf\\Application",
						"\\Yaf\\Dispatcher",
						"\\Yaf\\Controller_Abstract",
						"\\Yaf\\Bootstrap_Abstract",
						"\\Yaf\\Plugin_Abstract",
						"\\Yaf\\Request_Abstract",
						"\\Yaf\\Response_Abstract",
						"\\Yaf\\Registry",
					),
					$v);
			}
			$skeletons[$k] = $v;
		}
	}
}

function processOutput($directory, $skeletons) {
	if (!file_exists($directory)) {
		if (false === mkdir($directory, 0755, true)) {
			exit("$directory cannot be created");
		}
	}

	foreach ($skeletons as $k => $v) {
		if (is_array($v)) {
			@mkdir($directory . "/" . $k, 0755, true);
			processOutput($directory . "/" . $k, $skeletons[$k]);
		} else {
			file_put_contents($directory . "/" . substr($k, 0, -4), $v);
		}
	}
}

function getTemplates($root, &$templates = NULL) {
	$dir = opendir($root);
	if ($dir === false) {
		exit("Error: {$root} is not exsits\n");
	}
	if ($templates === NULL) {
		$templates = array();
	}
	while ($entry = readdir($dir)) {
		if ($entry[0] == "." || $entry[0] == "..") {
			continue;
		}
		if (!is_dir($root . $entry)) {
			$templates[$entry] = file_get_contents($root . $entry);
		} else {
			$templates[$entry] = array();
			getTemplates($root . $entry . "/", $templates[$entry]);
		}
	}
	return $templates;
}


echo "Generating done\n";
?>
